package org.hp.controller;

import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.hp.bean.User;
import org.hp.service.user.UserService;
import org.hp.util.Random;
import org.hp.util.ResultJson;
import org.hp.util.ReturnJsonData;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class UserController {
    @Resource
    UserService userSer;

    @RequestMapping("/UserQueryList")
    public void queryList(@Param("page") int page,@Param("limit") int limit,@Param("aname") String aname, HttpServletResponse response){//,@Param("aname") String aname,@Param("aphone") String aphone
        System.out.println("分页查询："+page+"，"+limit+"，aname："+aname);
        List<User> list = userSer.queryList(page, limit,aname);
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        ReturnJsonData.returnJson(ResultJson.success(pageInfo), response);
    }

    /*@RequestMapping("/UserQueryAll")
    public void queryAll( HttpServletResponse response){
        List<User> list = userSer.queryAll();
        ReturnJsonData.returnJson(ResultJson.success(list), response);
    }*/
    @RequestMapping("/UserQueryOne")
    public void queryOne(int aid,HttpServletResponse response){
        User user = userSer.queryOne(aid);
        ReturnJsonData.returnJson(ResultJson.success(user), response);
    }

    @RequestMapping("/UserUpdate")
    public void update(User user,HttpServletResponse response){
        int num = userSer.update(user);
        if (num > 0){
            ReturnJsonData.returnJson(ResultJson.success(), response);
        }else {
            ReturnJsonData.returnJson(ResultJson.error(), response);
        }
    }

    @RequestMapping("/UserPseudoDel")//伪删除的员工
    public void pseudoDel(int aid,HttpServletResponse response){
        int num = userSer.PseudoDel(aid);
        if (num > 0){
            ReturnJsonData.returnJson(ResultJson.success(), response);
        }else {
            ReturnJsonData.returnJson(ResultJson.error(), response);
        }
    }

    @RequestMapping("/PUserQueryAll")//伪删除查询
    public void pQueryAll(int page,int limit, @Param("auserId") String auserId,HttpServletResponse response){
        List<User> list = userSer.PqueryAll(page,limit,auserId);
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        ReturnJsonData.returnJson(ResultJson.success(pageInfo), response);
    }

    @RequestMapping("/PUserQueryOne")//伪删除的详情
    public void pQueryOne(int aid,HttpServletResponse response){
        User user = userSer.PqueryOne(aid);
        ReturnJsonData.returnJson(ResultJson.success(user), response);
    }

    @RequestMapping("/UserAdd")
    @Transactional
    public void add(User user,HttpServletResponse response){
        String auserId = Random.GetAUserId();
        user.setAuserId(auserId);
        user.setAtime(Random.GetATime());
        System.out.println("员工添加："+user);
        int num = userSer.add(user);
        int aid = userSer.addId(user);
        int addRole = userSer.addRole(aid);
        if ((num + addRole) == 2){

            ReturnJsonData.returnJson(ResultJson.success(), response);
        }else {
            ReturnJsonData.returnJson(ResultJson.error(), response);
        }
    }

    @RequestMapping("/UserRecover")//伪删除员工的恢复
    public void recover(int aid,HttpServletResponse response){
        int num = userSer.recover(aid);
        if (num > 0){
            ReturnJsonData.returnJson(ResultJson.success(), response);
        }else {
            ReturnJsonData.returnJson(ResultJson.error(), response);
        }
    }
    @RequestMapping("/UserAbandoned")//伪删除员工废弃
    public void abandoned(int aid,HttpServletResponse response){
        int num = userSer.Abandoned(aid);
        if (num > 0){
            ReturnJsonData.returnJson(ResultJson.success(), response);
        }else {
            ReturnJsonData.returnJson(ResultJson.error(), response);
        }
    }
}
